//
//  MyVC.m
//  WanCaiC8
//
//  Created by tangang on 2017/12/29.
//  Copyright © 2017年 tangang. All rights reserved.
//

#import "MyVC.h"
#import "MyCell.h"
#import "SettingVC.h"
#import "MyFansVC.h"
#import "MyCardVC.h"
#import "OnlineTop_up.h"
#import "OtherInfoVC.h"
#import "MyVM.h"
#import "IntegralVC.h"

@interface MyVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UIView      *headView;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@property (nonatomic ,strong) UIImageView   *headImage;
@property (nonatomic ,strong) QXLable       *labName;
@property (nonatomic ,strong) QXLable       *labSign;
@property (nonatomic ,strong) UIImageView   *sexImage;
@end

@implementation MyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backHome"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"setting"] style:UIBarButtonItemStylePlain target:self action:@selector(onclickSetUp)];
    self.navigationItem.rightBarButtonItem=rightItem;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)getData{
    WS(weakSelf);
    NSString *userId =[UserModel userModel].UserId;
    if (!userId) {
        [[BaseVM manage] login];
        return;
    }
    [[MyVM manage] getUserInfoWithUserId:userId WithSuccess:^(id returnValue) {
        [weakSelf updateUI];
        [UserModel setAvater:[UserInfoModel share].Avater];
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)updateUI{
    [self.tableView reloadData];
    UserInfoModel *model=[UserInfoModel share];
    _labName.text=model.NickName;
    [_headImage sd_setImageWithURL:[NSURL URLWithString:model.Avater] placeholderImage:[UIImage imageNamed:@"avatar"]];
    if (model.Sex) {
        _sexImage.image=[UIImage imageNamed:@"woman"];
    }else{
        _sexImage.image=[UIImage imageNamed:@"man"];
    }
    _labSign.text=model.Autograph;
    
}

#pragma mark -- 设置
- (void)onclickSetUp{
    SettingVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"SettingVC"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 在线充值
- (void)onclickTopUp{
    OnlineTop_up *vc =[[OnlineTop_up alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 进入个人信息
- (void)onclickUserInfo{
//    OtherInfoVC     *vc=[[OtherInfoVC alloc]init];
//    vc.userId=[UserModel userModel].UserId;
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark --
- (void)tag:(UITapGestureRecognizer *)tap{
    if (tap.view.tag==0) {
        [self onclickTopUp];
    }else if (tap.view.tag==1){
        IntegralVC *vc=[[IntegralVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag==2) {
        MyFansVC *vc=[[MyFansVC alloc]init];
        vc.type=FansTypeFocus;
        vc.title=@"我的关注";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag==3) {
        MyFansVC *vc=[[MyFansVC alloc]init];
        vc.type=FansTypeDefault;
        vc.title=@"我的粉丝";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag==4){
        MyCardVC *vc=[[MyCardVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    if (indexPath.row==0) {
        [cell setDict:self.dataArr[indexPath.row] andNum:[UserInfoModel share].Fans];
    }else if (indexPath.row==1){
        [cell setDict:self.dataArr[indexPath.row] andNum:[UserInfoModel share].Follow];
    }
    else{
        [cell setDict:self.dataArr[indexPath.row] andNum:@""];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        MyFansVC *vc=[[MyFansVC alloc]init];
        vc.type=FansTypeDefault;
        vc.title=@"我的粉丝";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row==1) {
        MyFansVC *vc=[[MyFansVC alloc]init];
        vc.type=FansTypeFocus;
        vc.title=@"我的关注";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==3){
        [SVProgressHUD showImage:nil status:@"该功能仅提供给本站签约彩票高手使用"];
    }
    else{
        NSDictionary *dict;
        dict=self.dataArr[indexPath.row];
        Class cl = NSClassFromString([dict objectForKey:@"vc"]);
        RootViewController *vc = [[cl alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight=80;
        [_tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.view);
            make.top.equalTo(self.headView.mas_bottom);
        }];
    }
    return _tableView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, real_size(184))];
        [self.view addSubview:_headView];
        _headView.backgroundColor=NAVCOLOR;
        _headImage=[[UIImageView alloc]init];
        _headImage.layer.masksToBounds=YES;
        _headImage.layer.cornerRadius=40;
        _headImage.layer.borderColor=[UIColor whiteColor].CGColor;
        _headImage.layer.borderWidth=3.0;
        [_headImage sd_setImageWithURL:[NSURL URLWithString:[UserModel userModel].Avater]];
        [_headView addSubview:_headImage];
        [_headImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headView);
            make.top.equalTo(_headView).with.offset(real_size(25));
            make.size.mas_equalTo(CGSizeMake(real_size(80), real_size(80)));
        }];
        
        QXLable *labName=[[QXLable alloc]init];
        labName.font=[UIFont boldSystemFontOfSize:18];
        labName.textAlignment=NSTextAlignmentRight;
        labName.text=[UserModel userModel].NickName;
        labName.textColor=[UIColor whiteColor];
        CGFloat width=[TGTools boundingRectWithString:labName.text withWidth:SCREEN_WIDTH anfont:labName.font];
        [_headView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headImage.mas_bottom).with.offset(5);
            make.centerX.equalTo(_headImage).with.offset(-15);
            make.size.mas_equalTo(CGSizeMake(width+10, 30));
        }];
        
        UIImageView *sexImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"man"]];
        [_headView addSubview:sexImage];
        [sexImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(labName);
            make.left.equalTo(labName.mas_right).with.offset(10);
            make.size.mas_equalTo(sexImage.image.size);
        }];
        
        _labSign=[[QXLable alloc]init];
        _labSign.font=[UIFont systemFontOfSize:13];
        _labSign.textColor=[UIColor whiteColor];
        _labSign.numberOfLines=2;
        _labSign.textAlignment=NSTextAlignmentCenter;
        [_headView addSubview:_labSign];
        [_labSign mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headView);
            make.top.equalTo(labName.mas_bottom).with.offset(10);
        }];
    }
    return _headView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]initWithObjects:
                  [[NSDictionary alloc]initWithObjectsAndKeys:@"我的粉丝",@"title",[UIImage imageNamed:@"pc_task"],@"image",@"MoneTaskVC",@"vc", nil],
                  [[NSDictionary alloc]initWithObjectsAndKeys:@"我的关注",@"title",[UIImage imageNamed:@"pc_reward"],@"image",@"MyCommissionVC",@"vc", nil],
                  [[NSDictionary alloc]initWithObjectsAndKeys:@"邀请注册",@"title",[UIImage imageNamed:@"pc_join"],@"image",@"InvitationRegisterVC",@"vc", nil],
                  [[NSDictionary alloc]initWithObjectsAndKeys:@"发布计划",@"title",[UIImage imageNamed:@"pc_join"],@"image",@"InvitationRegisterVC",@"vc", nil],
                  nil];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
